package com.hepinghospital.user.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.callback.SuccessCallback;
import com.kingja.loadsir.core.Convertor;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.hepinghospital.user.ui.widget.EmptyCallback;
import com.hepinghospital.user.ui.widget.ErrorCallback;
import com.hepinghospital.user.util.ListDataUiState;

import org.jetbrains.annotations.NotNull;

import butterknife.ButterKnife;

/**
 *
 */
public abstract class BaseLoadsirFragment extends Fragment {

    protected Activity mContext;

    protected LoadService mBaseLoadService;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        View rootView = inflater.inflate(getLayoutResource(), container, false);
        ButterKnife.bind(this, rootView);
        initData();
        initView();
        mBaseLoadService = LoadSir.getDefault().register(rootView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                onNetReload(v);
            }
        }, new Convertor<ListDataUiState>() {
            @Override
            public Class<? extends Callback> map(ListDataUiState state) {
                Class<? extends Callback> resultCode;
                if (state.isSuccess()) {
                    if (state.isFirstEmpty()) {
                        resultCode = EmptyCallback.class;
                    } else {
                        resultCode = SuccessCallback.class;
                    }
                } else {
                    resultCode = ErrorCallback.class;
                }
                return resultCode;
            }
        });
        return mBaseLoadService.getLoadLayout();
    }

    //获取布局文件
    protected abstract int getLayoutResource();


    //初始化view
    protected abstract void initView();

    protected abstract void initData();

    protected abstract void onNetReload(View v);

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(requireActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(requireActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void onAttach(@NotNull Context context) {
        mContext = (Activity) context;
        super.onAttach(context);
    }
}


